package book.controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;

import book.po.Book;
import book.service.BookService;
import book.view.BookDialog;
import book.view.BookFrame;

/**
 * 图书控制器
 * MVC的controller类
 * 该Controller是BookFrame和BookDialog共用的Controller。
 * @author Dengcj
 *
 */
public class BookControllerListener implements ActionListener{
	private BookFrame bookFrame;
	private BookDialog bookDialog;
	private BookService bookService;
	public BookControllerListener() {
		bookService = new BookService();
	}
	/**
	 *事件处理
	 *图书增删改查已全部实现！
	 *
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		//打开新增对话框
		if("addBookDialog".equals(e.getActionCommand())) { 
			bookDialog = new BookDialog(bookFrame,"添加图书",null);	
		}
		
		//新增提交
		else if("addPost".equals(e.getActionCommand())) { 
			Book newBook = this.getBookInstance();
			if(newBook != null) {
				bookService.addBook(newBook);
				JOptionPane.showMessageDialog(bookFrame, "添加"+newBook.getIsbn()+"号图书成功！");
				bookDialog.dispose();
				bookFrame.refresh(bookService.findAllBook());
			}else {
				JOptionPane.showMessageDialog(bookFrame, "添加图书失败！");
			}
			
		}
		
		//打开查询对话框
		else if("searchBookDialog".equals(e.getActionCommand())) { 
			List<Book> bookList = new ArrayList<Book>(); //临时集合
			for(Book book : bookService.findAllBook()) {
				bookList.add(book);
			}
			String str = JOptionPane.showInputDialog(bookFrame, "输入图书价格");
			if(str != null && !"".equals(str)) { //str不为null且str内容不为""
				if(str.matches("^[0-9]*$")){
					double price = Double.parseDouble(str);
					Book book = new Book();
					book.setPrice(price);
					List<Book> result = bookService.searchByPrice(bookList,book);
					bookFrame.showSearchResult(result);
				}else {
					JOptionPane.showMessageDialog(bookFrame, "输入的不是数字","输入错误",JOptionPane.ERROR_MESSAGE);
				}
			}
		}
		
		//打开修改对话框
		else if("updateBookDialog".equals(e.getActionCommand())) { 
			int selRow = bookFrame.getSelRow();
			if(selRow < 0) {
				JOptionPane.showMessageDialog(bookFrame, "请选择一行数据！");
			}else {
				Book book = bookService.findAllBook().get(bookFrame.getSelRow());
				bookDialog = new BookDialog(bookFrame,"修改图书",book);
			}
		}
		
		 //修改提交
		else if("updatePost".equals(e.getActionCommand())) { //修改提交
			Book updatedBook = this.getBookInstance();
			if(updatedBook != null) {
				bookService.updateBook(bookFrame.getSelRow(), updatedBook);
				JOptionPane.showMessageDialog(bookFrame, updatedBook.getIsbn()+"号图书修改成功！");
				bookDialog.dispose();
				bookFrame.refresh(bookService.findAllBook());
			}else {
				JOptionPane.showMessageDialog(bookFrame, "更新图书失败！");
			}
			
		}
		 //删除图书操作
		else if("deleteBook".equals(e.getActionCommand())) {
			int selRow = bookFrame.getSelRow();
			if(selRow <  0) {
				JOptionPane.showMessageDialog(bookFrame, "请选择一行数据！");
			}else {
//				Book book = bookService.getBook(selRow);//内存存储或文件存储
//				bookService.deleteBook(selRow); //内存存储或文件存储
				Book book = bookService.findAllBook().get(bookFrame.getSelRow());
				bookService.deleteBook(book.getIsbn());
				JOptionPane.showMessageDialog(bookFrame, "删除"+book.getIsbn()+"号图书成功！");
				bookFrame.refresh(bookService.findAllBook());
			}

		}
		
		//刷新图书列表窗口
		else if("refreshBookFrame".equals(e.getActionCommand())) {
			bookFrame.refresh(bookService.findAllBook());
		}
	}
	/**
	 * 获取图书vo实例
	 * Book为dialog传递的vo对象
	 * @return book
	 */
	public Book getBookInstance() {
		int isbn = 0;
		String name = null;
		double price = 0.0;
		Book book = null;
		String isbnInput  = bookDialog.getIsbnTextField().getText().trim();
		if(isbnInput != null && !"".equals(isbnInput)) {
			if(isbnInput.matches("^[0-9]*$")){
				isbn = Integer.parseInt(isbnInput);
			}
			else {
				JOptionPane.showMessageDialog(bookFrame, "ISBN必须是整数类型","输入错误",JOptionPane.ERROR_MESSAGE);
			}
		}else {
			JOptionPane.showMessageDialog(bookFrame, "ISBN不能为空","输入错误",JOptionPane.ERROR_MESSAGE);
		}
		String nameInput = bookDialog.getNameTextField().getText().trim();
		if(nameInput != null && !"".equals(nameInput)) {
			name = nameInput;
		}else {
			JOptionPane.showMessageDialog(bookFrame, "名称不能为空","输入错误",JOptionPane.ERROR_MESSAGE);
		}
		String priceInput = bookDialog.getPriceTextField().getText().trim();
		if(priceInput != null && !"".equals(priceInput)) {
			if(priceInput.matches("^[1-9][0-9]*([.][0-9]{1,2})?$")){
				price = Double.parseDouble(priceInput);
			}
			else {
				JOptionPane.showMessageDialog(bookFrame, "价格必须是数字类型","输入错误",JOptionPane.ERROR_MESSAGE);
			}
		}else {
			JOptionPane.showMessageDialog(bookFrame, "价格不能为空","输入错误",JOptionPane.ERROR_MESSAGE);
		}
		if(isbn > 0 && price>0 && name!=null) {
			book = new Book(isbn,name,price);
		}
		return book;
	}
	public void setBookFrame(BookFrame bookFrame) {
		this.bookFrame = bookFrame;
	}
	public BookService getBookService() {
		return bookService;
	}
	public void setBookAddDialog(BookDialog bookAddDialog) {
		this.bookDialog = bookAddDialog;
	}
	
}








